//
//  XDRsa.m
//  kabu
//
//  Created by XiangDongMBP on 2017/1/24.
//  Copyright © 2017年 East. All rights reserved.
//

#import "XDRsa.h"
#import "RSA.h"

NSString * const kPublicKey = @"-----BEGIN PUBLIC KEY-----MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDW5oag9XablzwXzdTUSypbo1UsJgbuA8DoYsD/LqwuMz53vHYvkndcfVsv/HuPQhZBTZZMvGyYpH5K2LFt0/T+UeJVtn6tAyL8ls8LZF+4YeiYE2pn7VQ9+3idKCd1A9LUqCbi0A87bXOf69/U/9VVP8++R/PqcIt4mQN5MPrJUQIDAQAB-----END PUBLIC KEY-----";

@implementation XDRsa
+ (NSString *)base64EncryptWithDictionary:(NSDictionary *)dic {
    if (![NSJSONSerialization isValidJSONObject:dic]) {
        return nil;
    }
    NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];
    return [self base64EncryptWithData:jsonData];
}

+ (NSString *)base64EncryptWithData:(NSData *)data {
//    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"public_key" ofType:@"pem"];
//    NSString *pubKey = [NSString stringWithContentsOfFile:filePath encoding:NSASCIIStringEncoding error:nil];
    NSData * encryptData = [RSA encryptData:data publicKey:kPublicKey];
    NSString * base64JsonStr = [encryptData base64EncodedStringWithOptions:0];
    return base64JsonStr;
}

@end
